﻿#include "MovieClip.prototype.as"
#include "String.prototype.as"

import flash.external.ExternalInterface;


function hide_tip( owner:Object )
{
	if( _root.tooltip._owner == owner )
		removeMovieClip("tooltip");
}

function show_tip( owner:Object, x:Number, y:Number, tip_title:String, tip_text:String )
{
	if( ( _root.tooltip != undefined ) )
	{
		if(_root.tooltip._owner==owner)
			return;	// <-- it's our tooltip and it is showing
		else
			removeMovieClip("tooltip");	// <-- it is someone elses tootlip - remove it
	}
		
	var tooltip = _root.createEmptyMovieClip( "tooltip", this.getNextHighestDepth() );
		
	// let the tooltip know who owns it, else we get weird race conditions where one
	// bar has onRollOver fired, then another has onRollOut and deletes the tooltip
	tooltip._owner = owner;
		
	var cstroke = {width:2, color:0x808080, alpha:100};
	var ccolor = {color:0xf0f0f0, alpha:100};

	tooltip.createTextField( "txt_title", tooltip.getNextHighestDepth(), 5, 5, 100, 100);
	tooltip.txt_title.text = tip_title;

	
	var fmt:TextFormat = new TextFormat();
	fmt.color = 0x0000F0;
	fmt.font = "Verdana";
	
	// this needs to be an option:
	fmt.bold = true;
	fmt.size = 12;
	fmt.align = "left";
	tooltip.txt_title.setTextFormat(fmt);
	tooltip.txt_title.autoSize="left";
	
	tooltip.createTextField( "txt", tooltip.getNextHighestDepth(), 5, tooltip.txt_title._height, 100, 100);
	
	//
	// Dirty hack. Takes a tool_tip_wrapper, and replaces the #val# with the
	// tool_tip text, so noew you can do: "My Val = $#val#%", which turns into:
	// "My Val = $12.00%"
	//
	if( _root.tool_tip_wrapper != undefined )
		tooltip.txt.text = _root.tool_tip_wrapper.replace('#val#',tip_text);
	else
		tooltip.txt.text = tip_text;
		
	var fmt2:TextFormat = new TextFormat();
	fmt2.color = 0x000000;
	fmt2.font = "Verdana";
	fmt2.size = 12;
	fmt2.align = "left";
	tooltip.txt.setTextFormat(fmt2);
	tooltip.txt.autoSize="left";
	
	var max_width:Number = Math.max( tooltip.txt_title._width, tooltip.txt._width );
	var y_pos:Number = y - tooltip.txt_title._height - tooltip.txt._height;
	
	if( y_pos < 0 )
	{
		// the tooltip has drifted off the top of the screen, move it down:
		y_pos = y + tooltip.txt_title._height + tooltip.txt._height;
	}

	tooltip.rrectangle(
		max_width+10,
		tooltip.txt_title._height + tooltip.txt._height + 5,
		6,
		((x+max_width+16) > Stage.width ) ? (Stage.width-max_width-16) : x,
		y_pos,
		cstroke,
		ccolor);
	
	// NetVicious, June, 2007
	// create shadow filter
	var dropShadow = new flash.filters.DropShadowFilter();
	dropShadow.blurX = 4;
	dropShadow.blurY = 4;
	dropShadow.distance = 4;
	dropShadow.angle = 45;
	dropShadow.quality = 2;
	dropShadow.alpha = 0.5;
	// apply shadow filter
	tooltip.filters = [dropShadow];
}

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// 菜单

function SetContextualMenu()
{
	var contextual_menu:ContextMenu = new ContextMenu();
	
    // 打印
	var menuPrint:ContextMenuItem = new ContextMenuItem("打印图形...");
	menuPrint.onSelect = function(obj, item)
	{
		var pj:PrintJob = new PrintJob();
		if (pj.start()) {

			// save original _root size
			var r:Object = {
		  		width: _root._width,
				height: _root._height
		  	};
			
		  	// choose scalefactor from larger dimension
			var caleFactor;
			if(r.width > r.height){
				scaleFactor = pj.pageWidth/r.width;
			} else {
				scaleFactor = pj.pageWidth/r.height;
			}
			// do the _root scaling
			_root._xscale = (scaleFactor*100) - 1;
		  	_root._yscale = (scaleFactor*100) - 1;
		  
			// add the page to the job and print
			if (pj.addPage(0, {xMin:0, xMax:Stage.width, yMin:0, yMax:Stage.height})) {
				pj.send();  // print page
			}
		  	// set original size back
		  	with(_root){
				_width = r.width;
				_height = r.height;
		  	}
	 	}
		delete pj;
	};	
	 
	contextual_menu.customItems.push(menuPrint);
	
	// 关于
	var menuAbout:ContextMenuItem = new ContextMenuItem("关于...");
	menuAbout.onSelect = function(obj, item) {
        getURL("javascript:popup=window.open('http://teethgrinder.co.uk/open-flash-chart/','ofc', 'toolbar=Yes,location=Yes,scrollbars=Yes,menubar=Yes,status=Yes,resizable=Yes,fullscreen=No'); popup.focus()");
	};
	contextual_menu.customItems.push(menuAbout);

	contextual_menu.hideBuiltInItems();
	 
	createClassObject
	_root.menu = contextual_menu;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// 
function ShowMessage( text:String )
{
	if( _root.mMessage != undefined )
	{
		HideMessage();
	}
	
	for(i in _root)
	{
		if(typeof(_root[i])=='movieclip')
		{
			removeMovieClip(_root[i]);
		}
		delete i;       
	}
	
	var mc:MovieClip = _root.createEmptyMovieClip( "mMessage", this.getNextHighestDepth() );
	mc.createTextField("txt", this.getNextHighestDepth(), 5, 5, 100, 100 );
	mc.txt.text = text;
	mc.ToolTip("owo");
	
	var fmt:TextFormat = new TextFormat();
	fmt.color = 0x000000;
	fmt.font = "Verdana";
	fmt.size = 12;
	fmt.align = "center";
	mc.txt.setTextFormat(fmt);
	mc.txt.autoSize="left";
	
	mc.txt.setTextFormat(fmt);
	
	var cstroke = {width:2, color:0x808080, alpha:100};
	var ccolor = {color:0xf0f0f0, alpha:100};
	//var stageheight = Stage.height/2;
	//if(_root.ChartFail == true) stageheight = 50;
	ChartUtil.RRectangle(
		mc,
		mc.txt._width+10,
		mc.txt._height+10,
		6,
		(Stage.width/2)-((mc.txt._width+10)/2),
                (Stage.height/2)-((mc.txt._height+10)/2),
		//stageheight-((mc.txt._height+10)/2),
		cstroke,
		ccolor);
	
	var dropShadow = new flash.filters.DropShadowFilter();
	dropShadow.blurX = 4;
	dropShadow.blurY = 4;
	dropShadow.distance = 4;
	dropShadow.angle = 45;
	dropShadow.quality = 2;
	dropShadow.alpha = 0.5;

	//if(_root.ChartFail == true)
	//{
	//    var swfname = _root.swfname;
        //    if(swfname != undefined)
        //    {
        //       ExternalInterface.call("ResizeSwf",swfname,0,100);
        //     }      
 	// }
}

function HideMessage()
{
	if(_root.mMessage != undifined) 
		removeMovieClip("mMessage");
}

///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

function LoadVarsOnLoad( success )
{
	if( !success  )
	{
		_root.ChartFail = true;
		_root.ShowMessage("加载数据失败");
		return;
	}

	for(i in _root)
	{
		if(typeof(_root[i])=='movieclip')
		{
			removeMovieClip(_root[i]);
		}
		delete i;       
	}
	
	if( this.Symptom != undefined) 
	{
	    _root.Chart = new MultiSymptom(this);
	}
	else if( this.Treatment != undefined) 
	{
	    _root.Chart = new MultiTreatment(this);
	}
	else if( this.Weight != undefined)
	{
		_root.Chart = new Weight(this);
	}
	else if( this.SingleTreatment != undefined)
	{
		_root.Chart = new SingleTreatment(this);
	}
	else if( this.SingleSymptom != undefined)
	{
		_root.Chart = new SingleSymptom(this);
	}
	else if( this.SingleWeight != undefined)
	{
		_root.Chart = new SingleWeight(this);
	}
	else if( this.SingleIndicator != undefined)
	{
		_root.Chart = new SingleIndicator(this);
	}
	else if(this.PieStyle != undefined)
	{
		_root.Chart = new PieStyle(this);
	}
	else if(this.Bar != undefined)
	{
		_root.Chart = new Bar(this);
	}	
	
	if(_root.ChartFail == true)
	{
		_root.ShowMessage("数据格式错误");
	}

	if( this.tool_tip != undefined )
	{
		_root.tool_tip_wrapper = this.tool_tip;
	}
	
	_root.loading.done();
	_root.move();	
}

///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

//菜单
SetContextualMenu();

Stage.align = "LT";
Stage.scaleMode = "noScale";

//System.useCodepage = true;

ShowMessage('正在加载数据......');

var mDataset:LoadVars = new LoadVars();
mDataset.onLoad = LoadVarsOnLoad;
mDataset.MakeChart = MakeChart;

_root.width = 1000;
if( _root.data == undefined )
{
	if( _root.variables == undefined )
	{
		_root.data="Weight.txt";
		mDataset.load(_root.data);
	}
	else
	{
		_root.LoadVarsOnLoad = LoadVarsOnLoad;
		_root.LoadVarsOnLoad( true );
	}
}
else
{
	_root.data = _root.data.Replace('|','&');
	mDataset.load(_root.data);
}

stop();